const fs = require('fs');
const eventstream = require('event-stream');
const os = require('os');
const gulp = require('gulp');
const gulpSequence = require('gulp-sequence').use(gulp);
const cfg = global.cfg = require('./gulp/gulp.config');
const gulpClean = require('./gulp/gulp.clean');
const gulpJavascript = require('./gulp/gulp.javascript');
const gulpCss = require('./gulp/gulp.css');
const gulpHtml = require('./gulp/gulp.html');
const gulpImage = require('./gulp/gulp.image');
const gulpCopy = require('./gulp/gulp.copy');
const gulpDeploy = require('./gulp/gulp.deploy');
const connect = require('gulp-connect');
const gulpOpen = require('gulp-open')

gulp.task('connect', function () {
  connect.server({
    // host: '192.168.1.90',
    root: './dist',
    port: 4000,
    livereload: true
  });
})

gulp.task('open', function (done) {
  gulp.src('')
    .pipe(gulpOpen({
      app: os.platform() === 'linux' ? 'Google chrome' : (os.platform() === 'darwin' ? 'Google chrome' : (os.platform() === 'win32' ? 'chrome' : 'firefox')),
      uri: 'http://localhost:4000'
    }))
    .on('end', done);
});

gulp.task('clean', gulpClean);
gulp.task('build:js', gulpJavascript);
gulp.task('build:css', gulpCss);
gulp.task('build:html', gulpHtml);
gulp.task('build:images', gulpImage);
gulp.task('build:copy', gulpCopy);

// 发布
gulp.task('build', gulpSequence(
  'clean',
  'build:images',
  'build:css',
  'build:js',
  ['build:html', 'build:copy']
));
gulp.task('watch', function (done) {
  gulp.watch([
    "./app/**/*.*"
  ], { cwd: cfg.path.cwd }, ['build:js', 'build:images', 'build:css', 'build:html', 'build:copy'])
    .on('end', done);
});

// 开发
gulp.task('dev', ['build', 'open', 'connect', 'watch']);
